package com.ruoyi.cngj.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cngj.domain.CngjHonor;
import com.ruoyi.cngj.service.ICngjHonorService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 荣誉专利Controller
 * 
 * @author yuexl
 * @date 2024-06-27
 */
@Controller
@RequestMapping("/cngj/honor")
public class CngjHonorController extends BaseController
{
    private String prefix = "cngj/honor";

    @Autowired
    private ICngjHonorService cngjHonorService;

    //@RequiresPermissions("cngj:honor:view")
    @GetMapping()
    public String honor()
    {
        return prefix + "/honor";
    }

    /**
     * 查询荣誉专利列表
     */
    //@RequiresPermissions("cngj:honor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CngjHonor cngjHonor)
    {
        startPage();
        List<CngjHonor> list = cngjHonorService.selectCngjHonorList(cngjHonor);
        return getDataTable(list);
    }

    /**
     * 导出荣誉专利列表
     */
    //@RequiresPermissions("cngj:honor:export")
    @Log(title = "荣誉专利", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CngjHonor cngjHonor)
    {
        List<CngjHonor> list = cngjHonorService.selectCngjHonorList(cngjHonor);
        ExcelUtil<CngjHonor> util = new ExcelUtil<CngjHonor>(CngjHonor.class);
        return util.exportExcel(list, "荣誉专利数据");
    }

    /**
     * 新增荣誉专利
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存荣誉专利
     */
    //@RequiresPermissions("cngj:honor:add")
    @Log(title = "荣誉专利", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CngjHonor cngjHonor)
    {
        return toAjax(cngjHonorService.insertCngjHonor(cngjHonor));
    }

    /**
     * 修改荣誉专利
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CngjHonor cngjHonor = cngjHonorService.selectCngjHonorById(id);
        mmap.put("cngjHonor", cngjHonor);
        return prefix + "/edit";
    }

    /**
     * 修改保存荣誉专利
     */
    //@RequiresPermissions("cngj:honor:edit")
    @Log(title = "荣誉专利", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CngjHonor cngjHonor)
    {
        return toAjax(cngjHonorService.updateCngjHonor(cngjHonor));
    }

    /**
     * 删除荣誉专利
     */
    //@RequiresPermissions("cngj:honor:remove")
    @Log(title = "荣誉专利", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cngjHonorService.deleteCngjHonorByIds(ids));
    }

    /**
     * 改变位置
     */
    @Transactional
    @PostMapping("/honorMove")
    @ResponseBody
    public AjaxResult honorMove(String type,String id,String changeId)
    {
        CngjHonor cngjHonor = cngjHonorService.selectCngjHonorById(id);
        CngjHonor changeCngiHonor = cngjHonorService.selectCngjHonorById(changeId);
        int num =0;
        if("1".equals(type) || "2".equals(type)){
            //上移
            int currSort = cngjHonor.getHonorSort();
            cngjHonor.setHonorSort(changeCngiHonor.getHonorSort());
            changeCngiHonor.setHonorSort(currSort);
            num +=cngjHonorService.updateCngjHonor(cngjHonor);
            num +=cngjHonorService.updateCngjHonor(changeCngiHonor);
        }
        if("3".equals(type)){
            cngjHonor.setHonorSort(Integer.parseInt(changeId)+1);
            num +=cngjHonorService.updateCngjHonor(cngjHonor);
        }
        return toAjax(num);
    }

    /**
     * 启用停用banner素材
     */
    @Log(title = "启用停用荣誉专利素材")
    @PostMapping( "/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(String id,String honorState)
    {
        CngjHonor cngjHonor = new CngjHonor();
        cngjHonor.setId(id);
        cngjHonor.setHonorState(honorState);
        return toAjax(cngjHonorService.updateCngjHonor(cngjHonor));
    }

}
